package com.lingtianyu.tech.erbaoshen.core.provider.bean.registry;

import org.springframework.aop.framework.ProxyFactory;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.util.StringUtils;

class ScannerFactoryBean implements FactoryBean<Object> {
    private ProxyFactory factory;
    private String interfaceClassName;

    public void setInterfaceClassName(String interfaceClassName) {
        this.interfaceClassName = interfaceClassName;

        try {
            factory.setInterfaces(Class.forName(interfaceClassName));
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public ScannerFactoryBean() {
        factory = new ProxyFactory(); //Spring代理工厂  
        factory.addAdvice(new CustomMethodInterceptor());
    }

    @Override
    public Object getObject() throws Exception {
        return factory.getProxy();
    }

    @Override
    public Class<?> getObjectType() {
        try {
            if (StringUtils.hasText(interfaceClassName)) {
                return Class.forName(interfaceClassName);
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public boolean isSingleton() {
        return true;
    }
}  